package com.hand.hzero.api.controller.v1;

import com.hand.hzero.config.SwaggerTags;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hzero.boot.platform.code.builder.CodeRuleBuilder;
import org.hzero.core.util.Results;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author hand-wd
 */
@Api(tags = SwaggerTags.EXAMPLE)
@RestController("codeRuleController.v1")
@RequestMapping("/v1/code/rule")
public class CodeRuleController {
    @Resource
    private CodeRuleBuilder codeRuleBuilder;

    @ApiOperation(value = "获取编码规则值")
    @Permission(permissionLogin = true)
    @GetMapping("/get-global")
    public ResponseEntity<String> getCodeRuleValue() {
        // 规则代码
        String ruleCode = "HPFM.TEST";
        // 变量替换列表
        Map<String, String> variableMap = new HashMap<>(16);
        // 生成全局编码规则
        String globalRuleCode = codeRuleBuilder.generateCode(ruleCode, variableMap);
        return Results.success("全局编码规则值为：" + globalRuleCode);
    }

    @ApiOperation(value = "批量获取编码规则值")
    @Permission(permissionLogin = true)
    @GetMapping("/get-global-beach")
    public ResponseEntity<List<String>> getCodeRuleValueBeach() {
        // 规则代码
        String ruleCode = "HPFM.TEST";
        // 批量生成编码的数量
        int quantity = 10;
        // 编码配置为变量时使用，参数类型为Map里面存储变量名以及对应变量值
        Map<String, String> variableMap = new HashMap<>(16);
        // 生成全局编码规则
        List<String> code = codeRuleBuilder.generateCode(quantity, ruleCode, variableMap);
        // 优化
        // List<String> l = codeRuleBuilder.batchGenerateCode(quantity, ruleCode, variableMap);
        return Results.success(code);
    }

    @ApiOperation(value = "生成租户级编码规则")
    @Permission(permissionLogin = true)
    @GetMapping("/get-tenant/{tenantId}")
    public ResponseEntity<String> getCodeRuleValueTenant(@PathVariable("tenantId") Long tenantId) {
        // 规则代码
        String ruleCode = "HPFM.TEST";
        // 编码规则层级 GLOBAL/COMPANY/CUSTOM(层级编码)
        String levelCode = "TEST";
        // 编码规则层级值
        String levelValue = "ZJ";
        // 编码配置为变量时使用，参数类型为Map里面存储变量名以及对应变量值
        Map<String, String> variableMap = new HashMap<>(16);
        // 生成全局编码规则
        String code = codeRuleBuilder.generateCode(tenantId, ruleCode, levelCode, levelValue, variableMap);
        return Results.success(code);
    }

    @ApiOperation(value = "生成租户级编码规则")
    @Permission(permissionLogin = true)
    @GetMapping("/get-custom/{tenantId}")
    public ResponseEntity<String> getCodeRuleValueCustom(@PathVariable("tenantId") Long tenantId) {
        // 编码针对不同层级的生成
        // 可选PLATFORM(P，序列全局唯一)、TENANT(T，序列按照指定的租户唯一)、COMPANY(C，序列按照指定的公司)唯一和CUSTOM(CT，序列按照指定的层级值唯一)
        String level = "T";
        // 规则代码
        String ruleCode = "HPFM.TEST";
        // 编码规则层级 GLOBAL/COMPANY/CUSTOM(层级编码)
        String levelCode = "TEST";
        // 编码规则层级值
        String levelValue = "ZJ";
        // 编码配置为变量时使用，参数类型为Map里面存储变量名以及对应变量值
        Map<String, String> variableMap = new HashMap<>(16);
        // 生成全局编码规则
        String code = codeRuleBuilder.generateCode(level, tenantId, ruleCode, levelCode, levelValue, variableMap);
        return Results.success(code);
    }
}
